import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../presentation/providers/auth_provider_web.dart'; // 使用Web专用认证提供者

/// Web专用应用初始化器（仅支持在线功能）
class AppInitializerWeb extends ConsumerStatefulWidget {
  final Widget child;

  const AppInitializerWeb({super.key, required this.child});

  @override
  ConsumerState<AppInitializerWeb> createState() => _AppInitializerWebState();
}

class _AppInitializerWebState extends ConsumerState<AppInitializerWeb> {
  bool _isInitialized = false;

  @override
  void initState() {
    super.initState();
    _initializeApp();
  }

  Future<void> _initializeApp() async {
    try {
      // Web端不需要初始化数据库和离线相关服务
      // 只需要检查认证状态（在线模式）
      await ref.read(authStateProviderWeb.notifier).checkAuthStatus();

      setState(() {
        _isInitialized = true;
      });
    } catch (e) {
      // 处理初始化错误
      debugPrint('Web应用初始化失败: $e');
      setState(() {
        _isInitialized = true;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    if (!_isInitialized) {
      return const MaterialApp(
        home: Scaffold(
          body: Center(
            child: CircularProgressIndicator(),
          ),
        ),
      );
    }

    return widget.child;
  }
}
